// JavaScript Document

function autoSlideUp(e){

	var $this = $('.shopcart-list');
		window.mouseX = e.pageX;
		window.mouseY = e.pageY;
	if( !((window.mouseX > 1080 && window.mouseX < 1300) &&
		(window.mouseY > 220 && window.mouseY < 659) ) ){
		if($this.hasClass('active-shopcart')){
			//console.log('start time out');
			window.timer = setTimeout(function(){
				$this.slideUp('fast',function(){
					$this.removeClass('active-shopcart');
				});
			},2000);
		}
	}
}

$(document).ready(function(){
	var wHeight = $(window).height();
	var wWidth = $(window).width();
	var timeAni = 250;
	window.timer = "";
	
	
	
	$('.shopcart-list').hover(
		function(){
			clearTimeout(window.timer);
			//console.log('cleared');
		},
		function(e){
			//console.log(e);
			autoSlideUp(e);
		}
	);	
	
	//updateShopcartHeight();
	
	//Local scroll for the page
	$('.content-right').localScroll();
	
	//Find and replace "placeholder" attribute for unsupport browsers
	$('input[placeholder], textarea[placeholder]').placeholder();
	
	//Generate an scrollbar for shopcart
	$('#scrollbar1').tinyscrollbar();
	
	//Toggle class and sliding for the category
	$('.table-category').live("click",function(){
		$(this).toggleClass('table-category-active');
		$(this).siblings('.table-category-content').slideToggle();	
	})
	
	//Delete each cart
	$('.eachClose').live("click",function(){
		$parent = $(this).parent('.eachCart');
		$parent.slideUp('fast',function(){
			$parent.remove();			
			updateScrollbar();
		})
		// If there's only ONE item, them close the shopcart
		if($('.eachCart').length == 1)
			$('.shopcart-list').slideUp('fast',function(){
				$(this).removeClass('active-shopcart');
			});
		
	})
	
	//initMenu
	initMenu();
	
	//Toggle the search group panel
	$('#header-search-group').live("click",function(event){		
			$(this).toggleClass('activeHeaderSeachGroup');
			$('#header-search-group-wrapper').slideToggle();
		
		event.stopPropagation();
	});
	$('#header-search-group-wrapper ul li a').live("click",function(event){
		event.stopPropagation();
	});
	$('.header, .content').live("click",function(){				
			var $slide = $('#header-search-group-wrapper'),
				$btn = $('#header-search-group');
			
			if($btn.hasClass('activeHeaderSeachGroup') && !$slide.is(':animated')){
				$slide.slideToggle();
				$btn.toggleClass('activeHeaderSeachGroup');			
			}		
	});
	
	//Change the quantity
	$('.btnDown, .btnUp').live("click",function(){
		if($(this).attr('class') == 'btnDown'){
			var $input = $(this).next('input');
			var val = parseInt($input.val()) - 1;
			if(val < 1)
				$input.val('1');
			else $input.val(val);			
		}else{
			var $input = $(this).prev('input');
			$input.val(parseInt($input.val())+1);
		}		
	})
	
	//Open the shopcart
	$('.table-order').live("click",function(){
		$parent = $(this).parent('.table-col-7').parent('.table-row');
		if(!$parent.hasClass('sold-out')){
			$parent.addClass('item-added');
			$('.shopcart-list').slideDown('fast',function(){
				//After finish sliding down, active class then trigger the "mouse out" event on shopcart
				$(this).addClass('active-shopcart');
				$(".shopcart-list").trigger('mouseout');
				
						
			});
			$('#scrollbar1').tinyscrollbar();
		}
		
	})
	
	$('#nav-sc-r').live("click",function(){
		$('.shopcart-list').slideToggle('fast',function(){
				//After finish sliding down, active class then trigger the "mouse out" event on shopcart
				$(this).addClass('active-shopcart');
				$(".shopcart-list").trigger('mouseout');
				
						
			});
		$('#scrollbar1').tinyscrollbar();	
	})
	
	//Close the shopcart
	$('#btnShopcart-close').live("click",function(){
		$('.shopcart-list').slideUp('fast',function(){
			$(this).removeClass('active-shopcart');
		});
	});
	
	//Slider style LARGE
	$('.slide-wrapper').cycle({ 
		fx:    'fade', 
    	delay: -1000,
		speed:2000,
		timeout:3000		
	});
	
	//Hover effect for table rows
	$('.table-category').hover(
			function(){$(this).animate({backgroundColor:'#86c2ff'},'fast');},
			function(){$(this).animate({backgroundColor:'#bdddf8'},'fast');}
	);	
	$('.table-row').hover(
			function(){$(this).animate({backgroundColor:'#edecf0'},'fast');},
			function(){$(this).animate({backgroundColor:'#FFF'},'fast');}
	);
	$('#intro-category ul li a').hover(
			function(){
				$(this).animate({backgroundColor:'#000000'},'fast');
			},
			function(){
				
				$(this).animate({backgroundColor:'#ef383c'},'fast');
			}
	);
	
});

//Update scrollbar whenever a item has been added/removed from the shopcart
function updateScrollbar(){
	$('#scrollbar1').tinyscrollbar();	
}

function updateShopcartHeight(){
	var minH = 57;
	var height = minH;
	var cart = $('.eachCart').length;
	//alert(cart);
	var $view = $('.viewport');
	if(cart >= 10)
		height = 456;
	else height = cart * minH;
	
	$view.css({'height':height});
}

//Sliding category
function initMenu(){
	/* For slide uo/down effect */
	/*
	 $('ul.main-order-list ul').hide();
	 var $activeClass = 'activeCate';	 
	 $('ul.main-order-list li a').click(function(){
		 
		 $('ul.main-order-list li a').removeClass($activeClass);	
		 $(this).addClass($activeClass);

		var checkElement = $(this).next('.sub-list-order-2');
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			 $('ul.main-order-list ul:visible').slideUp('normal');
			checkElement.slideUp('normal');
			$(this).removeClass($activeClass);
			return false;
			}
		
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('ul.main-order-list ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
			}
			
		});
		*/
	$('a.activeCate').next('ul.sub-list-order-2').css({'display':'block'});
	
	}